require 'combination'

class CreateRicalModels < ActiveRecord::Migration
  def self.up
    #A collection has many calendars, and a calendar may belong to many collections
    create_table "rical_calendar_collections", :id => false do |t|
      t.integer :collection_id
      t.integer :calendar_id
      t.timestamps
    end
    
    #A collection is a set of calendars
    create_table "rical_collections" do |t|
      t.string :name
      t.integer :owner_id
      t.timestamps
    end
    
    #A calendar is a set of events
    create_table "rical_calendars" do |t|
      t.string :name
      t.text :description
      t.integer :owner_id
      t.string :calscale #eg gregorian
      t.string :method #eg publish
      t.string :tzid #eg America/New_York
      t.string :tzname #eg EDT
      t.timestamps
    end
    
    #An event may have recurring rules and belongs to one and only one calendar
    create_table "rical_events" do |t|
      t.integer :calendar_id
      t.string :name
      t.text :description
      t.integer :dtstart #will store as utc integer and convert manually(Time.at faster than Time.parse)
      t.integer :dtend #will store as utc integer and convert manually(Time.at faster than Time.parse)
      t.string :uid
      t.integer :visibility #enumeration(public/private)
      t.integer :rrule_freq #enumeration(eg WEEKLY, DAILY, MONTHLY)
      t.integer :rrule_wkst #enumeration(eg SU, MO, TU)
      t.integer :rrule_until #will store as utc integer and convert manually(Time.at faster than Time.parse)
      t.integer :rrule_byday_id
      t.integer :rrule_bymonth_id
      t.string :location
      t.integer :sequence
      t.integer :status #enumeration
      t.integer :transparency#enumeration
      t.timestamps
    end
    
    #enumeration table that will hold all the combinations of by month
    create_table "rical_rrule_bymonth" do |t|
      #recurring rule may have any combination of months
      t.boolean "m1"
      t.boolean "m2" 
      t.boolean "m3"
      t.boolean "m4" 
      t.boolean "m5" 
      t.boolean "m6" 
      t.boolean "m7" 
      t.boolean "m8" 
      t.boolean "m9" 
      t.boolean "m10" 
      t.boolean "m11" 
      t.boolean "m12" 
    end

    #generate all possible combinations of bymonth's
    bymonth_klass = "rical/rrule_bymonth".camelcase.constantize
    gen_all_possible_combinations(bymonth_klass, "m", 1)
    
    #enumeration table that will hold all the combinations by day of the week
    create_table "rical_rrule_byday" do |t|
      #recurring rule may have any combination of days of the week
      t.boolean "d0"
      t.boolean "d1"
      t.boolean "d2"
      t.boolean "d3"
      t.boolean "d4"
      t.boolean "d5"
      t.boolean "d6"
    end   
    byday_klass = "rical/rrule_byday".camelcase.constantize
    gen_all_possible_combinations(byday_klass, "d", 0)
    


  end

  def self.down
    drop_table "rical_collections"
    drop_table "rical_calendars"
    drop_table "rical_events"
    drop_table "rical_calendar_collections"
    drop_table "rical_rrule_bymonth"
    drop_table "rical_rrule_byday"
    
  end
  
  def self.gen_all_possible_combinations(klass, field_prefix, offset)
    columns = klass.column_names
    columns.length.times do |i|
      combos = Rical::Combination.get(columns.length-1, i+1)
      combos.each do |combo|
        k = klass.new
        combo.each do |cell|
          k.send("#{field_prefix}#{cell+offset}=", true)
        end
        k.save!
      end

    end
  end

end
